﻿class PaintLoader extends egret.EventDispatcher {

    public paint: any;
    private loader: LoaderManager;

    public initData(_paint: any,index:number): void {
        this.paint = _paint;
        this.type = QNService.checktype(this.paint.data, this.paint.audio);

        this.loader = new LoaderManager();
        this.loader.init();

        this.loader.errorHandler = this.onError;
        this.loader.addEventListener(egret.Event.COMPLETE,() => {
            this.dispatchEvent(new egret.Event(egret.Event.COMPLETE));
        }, this);

        setTimeout(() => { this.startLoad() }, index*500)
    }

    public startLoad(): void {
        var dataUrl: string = this.paint.data;
        if (dataUrl&&dataUrl.indexOf(VO.VIDEO_NAME_MP4)<0) {
            var dataLoader: egret.URLLoader = new egret.URLLoader();
            var dataReq: egret.URLRequest = new egret.URLRequest(QNService.getFullUrl(dataUrl));
            dataLoader.dataFormat = egret.URLLoaderDataFormat.TEXT;
            dataLoader.addEventListener(egret.Event.COMPLETE, this.onDataLoaded, this);
            dataLoader.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onError, this);
            dataLoader.load(dataReq);
        } else {
            var audioUrl: string = this.paint.audio;
            if (audioUrl) {
               // this.loader.append(audioUrl, egret.URLLoaderDataFormat.SOUND);
            }

            var coverUrl: string = this.paint.cover;
            if (coverUrl) {
                this.loader.append(coverUrl, egret.URLLoaderDataFormat.TEXTURE);
            }
            this.loader.startLoad();
        }
    }

    private onDataLoaded(event: egret.Event): void {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        var s = loader.data;
        if (s.indexOf('replayData') >= 0)
            this.loadAnime(s);
        else if (s.indexOf('quizArr') >= 0)
            this.loadQuiz(s);
        else
            this.loader.startLoad();
    }

    public animeData: any;

    private loadAnime(s: string): void {
        console.log("startload anime");

        this.animeData = JSON.parse(s);
        var tarr: any[] = this.animeData.textures;
        for (var i: number = 0; i < tarr.length; i++) {
            this.loader.append(tarr[i], egret.URLLoaderDataFormat.TEXTURE);
        }

        var audioUrl: string = this.paint.audio;
        if (audioUrl) {
           //this.loader.append(audioUrl, egret.URLLoaderDataFormat.SOUND);
        }

        var coverUrl: string = this.paint.cover;
        if (coverUrl) {
            this.loader.append(coverUrl, egret.URLLoaderDataFormat.TEXTURE);
        }

        var background: string = this.animeData.background;
        if (background) {
            this.loader.append(background, egret.URLLoaderDataFormat.TEXTURE);
        }

        this.loader.startLoad(); 
    }

    public quizData: any;

    private loadQuiz(s: string): void {
        console.log("startload quiz");

        this.quizData = JSON.parse(s);
        var qarr: any[] = this.parseQuizs(this.quizData.quizArr);
        this.quizData.quizArr = qarr;

        var coverUrl: string = this.paint.cover;
        if (coverUrl) {
            this.loader.append(coverUrl, egret.URLLoaderDataFormat.TEXTURE);
        }

        var audioUrl: string = this.paint.audio;
        if (audioUrl) {
            //this.loader.append(audioUrl, egret.URLLoaderDataFormat.SOUND);
        }

        for (var i: number = 0; i < qarr.length; i++) {
            var obj: any = qarr[i];
            if (obj.type==0)
            {
                var quiz: any = obj;

               // this.loader.append(quiz.snd, egret.URLLoaderDataFormat.SOUND);
               // this.loader.append(quiz.rightSnd, egret.URLLoaderDataFormat.SOUND);
                //this.loader.append(quiz.wrongSnd, egret.URLLoaderDataFormat.SOUND);
                this.loader.append(quiz.src, egret.URLLoaderDataFormat.TEXTURE);
            }
        }

        this.loader.startLoad();
    }

    private parseQuizs(arr: any[]): any[] {
        arr = arr.map(function (item:any, index: number, array: any[]): any {
            var quiz: any;
            switch (item.type) {
                case 0:
                    {
                        quiz = {};
                        quiz.src = item.src;
                        quiz.rect = new egret.Rectangle(item.rect.x, item.rect.y, item.rect.width, item.rect.height);
                        quiz.snd = item.snd;
                        quiz.rightSnd = item.rightSnd;
                        quiz.wrongSnd = item.wrongSnd;
                        quiz.type = item.type;
                        break;
                    }

                default:
                    {
                        break;
                    }
            }
            return quiz;
        });
        return arr;
    }

    private onError(): void {
        this.dispatchEvent(new egret.IOErrorEvent(egret.IOErrorEvent.IO_ERROR));
    }

    public data: any;
    public type: string;

    public getRes(key:string): any {
        return this.loader.getRes(key);
    }
} 